home *** CD-ROM | disk | FTP | other *** search
Java Source | 1997-07-18 | 5.1 KB | 213 lines |
- // NcgiServletGate - NCGI program that runs Servlets
- //
- // Copyright (c) 1996 Sun Microsystems, Inc. All Rights reserved
- // Permission to use, copy, modify, and distribute this software
- // and its documentation for NON-COMMERCIAL purposes and without
- // fee is hereby granted provided that this copyright notice
- // appears in all copies. Please refer to the file copyright.html
- // for further important copyright and licensing information.
- //
- // SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
- // THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
- // TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
- // PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
- // ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
- // DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
-
- package sun.servlet.apache;
-
- import java.io.*;
- import java.util.*;
- import java.net.*;
- import javax.servlet.*;
- import javax.servlet.http.*;
-
- /** NCGI program that runs Servlets.
- ** <P>
- ** This is an NCGI program in java, but instead of actually serving
- ** content it instead turns the NCGI requests into Jeeves-compatible
- ** Servlet requests, and then runs the Servlets.
- */
-
- public class NcgiServletGate extends ServletGate {
-
- private static final int PORT = 31461;
- private static final String AUTHFILE = "/tmp/ncgiauth";
-
- public static void main( String[] args ) {
-
- // Parse args.
- int port = PORT;
- String authfile = AUTHFILE;
- String servletPropFile = null;
- int argc = args.length;
- int argn;
- for ( argn = 0; argn < argc && args[argn].charAt( 0 ) == '-'; ++argn ) {
- if ( args[argn].equals( "-p" ) && argn + 1 < argc ) {
- ++argn;
- port = Integer.parseInt( args[argn] );
- }
- else if ( args[argn].equals( "-a" ) && argn + 1 < argc ) {
- ++argn;
- authfile = args[argn];
- }
- else if (args[argn].equals( "-s" ) && argn + 1 < argc ) {
- ++argn;
- servletPropFile = args[argn];
- }
- else usage();
- }
- if ( argn != argc ) usage();
-
- // Create the server.
- NcgiServletGate serve = new NcgiServletGate(port, authfile,
- servletPropFile );
-
-
- // And run.
- serve.serve();
-
- // If we get here, we're done.
- System.exit( 0 );
- }
-
- private static void usage() {
- System.err.println( "usage: NcgiServletGate [-p port] [-a authfile]" );
- System.exit( 1 );
- }
-
- private int port;
- private String authfile;
-
- /**
- * Constructor.
- */
- public NcgiServletGate( int port, String authfile, String servletPropFile ) {
- super();
- this.port = port;
- this.authfile = authfile;
-
- // load initial servlet properties
- if (servletPropFile != null) {
- loadServletProps(servletPropFile);
- }
-
- }
-
- /**
- * Run the server. Returns only on errors.
- */
- public void serve() {
- NcgiServer ncgiServer = null;
- try {
- ncgiServer = new NcgiServer( port, authfile );
- while ( true ) {
- Socket socket = ncgiServer.accept();
- try {
- InputStream in = socket.getInputStream();
- OutputStream out = socket.getOutputStream();
- new NcgiServletGateConnection( this, in, out,
- ncgiServer, socket );
- }
- catch ( IOException e ) {
- log( "problem getting streams: " + e.toString() );
- }
- }
- }
- catch ( NcgiException e ) {
- log( e.toString() );
- }
- finally {
- if ( ncgiServer != null )
- ncgiServer.done();
- destroyAllServlets();
- }
- }
-
-
- // Methods from ServletContext.
-
- /**
- * The name of this software.
- */
- public String serverName() {
- return "sun.servlet.apache.ncgi.NcgiServletGate";
- }
-
- /**
- * The version of this software.
- */
- public String serverVersion() {
- return "v1.0beta1";
- }
-
- /**
- * The URL for this software.
- */
- public String serverUrl() {
- return "http://jeeves.javasoft.com/";
- }
-
- }
-
-
- class NcgiServletGateConnection extends ServletGateConnection
- implements Runnable {
- private NcgiServer ncgiServer;
- private Socket socket;
-
- /**
- * Constructor.
- */
- public NcgiServletGateConnection( ServletGate serve, InputStream in,
- OutputStream out, NcgiServer ncgiServer,
- Socket socket ) {
- super( serve, in, out );
-
- // Save arguments.
- this.ncgiServer = ncgiServer;
- this.socket = socket;
-
- // Start a separate thread to read and handle the request.
- Thread thread = new Thread( this );
- thread.start();
- }
-
-
- // Methods from Runnable.
-
- private NcgiRequest ncgiRequest;
-
- public void run() {
- try {
- // Read the request.
- ncgiRequest = new NcgiRequest( in, ncgiServer );
-
- // Handle it.
- handle();
- }
- catch ( NcgiException e ) {
- serve.log( e.toString() );
- }
- try {
- socket.close();
- }
- catch ( IOException ignore ) { }
- }
-
- /**
- * Returns a particular variable for this request.
- */
- public String getVar( String name ) {
- return ncgiRequest.getVar( name );
- }
-
- /**
- * Returns an enumeration of variables for this request.
- */
- public Enumeration getVars() {
- return ncgiRequest.getVars();
- }
-
- }
-